package com.qzimp.fujiancuo.controller;

import com.qzimp.fujiancuo.entity.Architecture;
import com.qzimp.fujiancuo.entity.Cuisine;
import com.qzimp.fujiancuo.entity.Region;
import com.qzimp.fujiancuo.entity.dto.RegionArcDto;
import com.qzimp.fujiancuo.entity.dto.RegionCuisineDto;
import com.qzimp.fujiancuo.entity.vo.ResultCode;
import com.qzimp.fujiancuo.entity.vo.ResultVo;
import com.qzimp.fujiancuo.exceptions.ApiException;
import com.qzimp.fujiancuo.service.ArchitectureService;
import com.qzimp.fujiancuo.service.CuisineService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Api("lwl的接口")
@RequestMapping("/linwenliang")
public class LinwenliangController {
    @Autowired
    private CuisineService cuisineService;
    @GetMapping("/food/region/{regionName}")
    @ApiOperation("通过地区名称查询美食信息")
    public ResultVo listCuisineByRegionName(@PathVariable @ApiParam("地区名称") String regionName) {
        Region region = cuisineService.selectByName(regionName);
        if (region == null) {
            throw new ApiException(ResultCode.REGION_ERROR);
        }
        List<Cuisine> cuisines = cuisineService.selectByRegionId(region.getId());
        if (cuisines == null || cuisines.isEmpty()) {
            throw new ApiException(ResultCode.CUISINE_NOT_FOUND);
        }
        RegionCuisineDto regionCuisineDto = new RegionCuisineDto(region, cuisines);
        return new ResultVo(ResultCode.SUCCESS,regionCuisineDto);
    }

    @Autowired
    private ArchitectureService architectureService;
    @GetMapping("/arc/region/{regionName}")
    @ApiOperation("根据地区名称查询景点建筑信息")
    public RegionArcDto getRegionArcByName(@PathVariable @ApiParam("地区名称") String regionName) {
        Region region = architectureService.selectByName(regionName);
        if (region == null) {
            throw new ApiException(ResultCode.REGION_ERROR);
        }
        List<Architecture> architectures = architectureService.selectByRegionId(region.getId());
        if (architectures == null || architectures.isEmpty()) {
            throw new ApiException(ResultCode.ARC_NOT_FOUND);
        }
        return new RegionArcDto(region, architectures);
    }
}
